{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a4d87b83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tf2.0版本\n",
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_eager_execution()         #可以用于从TensorFlow 1.x到2.x的复杂迁移项目的程序开头\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac481ebb",
   "metadata": {},
   "outputs": [],
   "source": [
    "    \n",
    "hello = tf.constant('hello,tf')\n",
    "#张两\n",
    "sess = tf.Session()\n",
    "print(sess.run(hello))\n",
    "\n",
    "#eval\n",
    "#注册默认会话\n",
    "sess = tf.InteractiveSession()\n",
    "print(hello.eval(session=sess))\n",
    "with sess.as_default():\n",
    "    print(hello.eval())\n",
    "    \n",
    "sess.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5c34da16",
   "metadata": {},
   "outputs": [],
   "source": [
    "node1 = tf.constant(3.0,tf.float32,name=\"n1\")\n",
    "node2 = tf.constant(4.0,tf.float32,name=\"n2\")\n",
    "node3 = tf.add(node1,node2)\n",
    "\n",
    "# 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "15ee65bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0\n",
      "7.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<bound method BaseSession.close of <tensorflow.python.client.session.Session object at 0x00000115A361EF08>>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(node3))\n",
    "    \n",
    "# 2\n",
    "\n",
    "sess = tf.Session()\n",
    "print(sess.run(node3))\n",
    "sess.close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "55bb84f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7. 3.]\n",
      " [6. 3.]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<bound method BaseSession.close of <tensorflow.python.client.session.Session object at 0x00000115A6676848>>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#变量,前面是常量 \n",
    "v1 = tf.Variable([[3.0,1.5],[3.0,1.5]],tf.float32)\n",
    "v2 = tf.Variable([[4.0,1.5],[3.0,1.5]],tf.float32)\n",
    "v3 = tf.add(v1,v2)\n",
    "v1,v2\n",
    "#创建， 个别变量初始化initializer（） 所有初始化\n",
    "# #1\n",
    "# with tf.Session() as sess:\n",
    "#     print(sess.run(v3))\n",
    "    \n",
    "# 2\n",
    "\n",
    "sess = tf.Session()\n",
    "#会话默认的sess变量初始化\n",
    "init = tf.global_variables_initializer()\n",
    "sess.run(init)\n",
    "\n",
    "\n",
    "print(sess.run(v3))\n",
    "sess.close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "220dff93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "6\n",
      "10\n",
      "15\n",
      "21\n",
      "28\n",
      "36\n",
      "45\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "value = tf.Variable(0,tf.float32)\n",
    "Sum = tf.Variable(0,tf.float32)\n",
    "one = tf.constant(1)\n",
    "new_value = tf.add(value,one)\n",
    "new_Sum = tf.add(Sum,value)\n",
    "update_value= tf.assign(value,new_value)\n",
    "update_sum= tf.assign(Sum,new_Sum)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for _ in range(10):\n",
    "        sess.run(update_value)\n",
    "        sess.run(update_sum)\n",
    "#         print(sess.run(value))\n",
    "        print(sess.run(Sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "cc763a2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Add_9:0\", shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "print(node3)\n",
    "#张量的结构tf1 计算过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c3cc0ef3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor 'Add_10:0' shape=(2, 2) dtype=float32>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = tf.constant([[3.0,1.5],[3.0,1.5]],tf.float32)\n",
    "n2 = tf.constant([[4.0,1.5],[3.0,1.5]],tf.float32)\n",
    "n3 = tf.add(n1,n2)\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "385fc4d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"n1_2:0\", shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "print(node1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "39a20cc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "print(sess.run(node1))\n",
    "try:\n",
    "    print(sess.run(node1))\n",
    "except:\n",
    "    print(\"print(sess.run(node1)) error!\")\n",
    "finally:\n",
    "    sess.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6c8c7acd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(node3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6cfa9bdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "sess.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "76095f2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = tf.Variable(1)\n",
    "b = tf.add(a,1)\n",
    "c = tf.multiply(b,4)\n",
    "d = tf.subtract(c,b)\n",
    "logdir = \"D:/jupyter Env/log\"\n",
    "writer = tf.summary.FileWriter(logdir,tf.get_default_graph())\n",
    "writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "b212daa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[28.0, 4.5]\n",
      "28.0\n"
     ]
    }
   ],
   "source": [
    "# tf2.0版本\n",
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_eager_execution()         #可以用于从TensorFlow 1.x到2.x的复杂迁移项目的程序开头\n",
    "\n",
    "\n",
    "a = tf.placeholder(tf.float32,name='a')\n",
    "b = tf.placeholder(tf.float32,name='b')\n",
    "c = tf.multiply(a,b,name='c')\n",
    "d = tf.subtract(a,b,name='d')\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "#     sess.run(init)\n",
    "    res = sess.run([c,d],feed_dict={a:8.0,b:3.5})\n",
    "    rc,rd = sess.run([c,d],feed_dict={a:8.0,b:3.5})\n",
    "    print(res)\n",
    "    print(res[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07dc57c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
